package com.module.prms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;

import base.util.ResolveUnit;

import com.activityClient.client.ActivityThread;
import com.module.prms.manager.RoleOperationManager;
import com.module.prms.vo.FMenuTree;
import com.module.prms.vo.MenuTree;
import com.module.prms.vo.PrmsMenu;
import com.module.prms.vo.PrmsOperation;
import com.module.prms.vo.PrmsRoleOperation;
import com.module.sys.ConstantUnit;
import com.module.sys.LoginSession;
import com.webClient.client.SocketClient;
import com.webClient.service.WebClientService;



public class orleoptService {
	static SocketClient sClient = null;
	static WebClientService ss = null;
	static Logger logger = Logger.getLogger(orleoptService.class.getName());

	private String getJson(Map map) throws Exception {
		String sessionId = UUID.randomUUID().toString();
		map.put(ConstantUnit.STATUS_NAME, ConstantUnit.STATUS_SEND);
		map.put(ConstantUnit.SESSIONID_NAME, sessionId);
		map.put(ConstantUnit.B_ID, "prms");
		map.put(ConstantUnit.CN, "PrmsRoleOperation");
		String json = ResolveUnit.getJsonStr(map);
		return json;
	}

	/**
	 * 返回功能菜单 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public List listmenuopt(String Pid) throws Exception {
		PrmsRoleOperation roleopt = new PrmsRoleOperation();
		roleopt.setCroleId(LoginSession.getRoleid());
		roleopt.setCorgId(Pid);
		RoleOperationManager rom = RoleOperationManager.getInstance();
		return rom.list(roleopt).getTree();
	}

	public List listroleopt(String roleid) throws Exception {
		RoleOperationManager rom = RoleOperationManager.getInstance();
		PrmsRoleOperation roleopt = new PrmsRoleOperation();
		roleopt.setCroleId(roleid);
		List tree=rom.listorleopt(roleid).getTree();
		return tree;
	}

	public Map save(List optlist, String roleid) {
		try {
			FMenuTree fmt = new FMenuTree();
			fmt.setTree(optlist);
			RoleOperationManager rom = RoleOperationManager.getInstance();
			boolean isTrue=rom.add(fmt,roleid);
			Map<String, Object> statusMap = new HashMap<String, Object>();
			statusMap.put("state", isTrue);
			if (isTrue == true) {
				statusMap.put("msg", "保存成功");
			} else {
				statusMap.put("msg", "保存失败");
			}
			return statusMap;
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}

	//获得按钮资源
	public List<String> getOperationByRoleIdAndMenuId(String menuId){
		List<String>  list = new ArrayList<String>();
		try {
			 String roleid=LoginSession.getRoleid();//角色id
			 System.out.println("roleid="+roleid);
			 RoleOperationManager roleOperationManager =  RoleOperationManager.getInstance();
			 list=roleOperationManager.getOperationByRoleIdAndMenuId(roleid, menuId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	//获得按钮资源
	public List<String> getOptByRoleIdAndMenuId(List menus){
		List<String>  list = new ArrayList<String>();
		try {
			 String roleid=LoginSession.getRoleid();//角色id
			 System.out.println("roleid="+roleid);
			 for(int i=0;i<menus.size();i++)
			 {
				 MenuTree mt = (MenuTree) menus.get(i);
				 List pmlist = mt.getChildren();
				 for (int j=0;j<pmlist.size();j++)
				 {
					 PrmsMenu pm = (PrmsMenu) pmlist.get(j);
					 RoleOperationManager roleOperationManager =  RoleOperationManager.getInstance();
					 List listt=roleOperationManager.getOperationByRoleIdAndMenuId(roleid, pm.getCmenuId());
					 list.addAll(listt);
				 }
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	

}
